﻿using System;
using System.Linq;
using System.Web.Mvc;
using Citi.Scheduler.Domain;
using Citi.Scheduler.WebUi.Models;

namespace Citi.Scheduler.WebUi.Controllers
{
    public class CategoryController : ControllerBase<CategoryModel, Category>
    {
        public ActionResult Index()
        {
            var categories = new Category().SelectAllActive<Category>().Where(x => !x.ParentId.HasValue);
            var models = new CategoryModel().CreateModel(categories);
            return PartialView(models);
        }

        public ActionResult Form()
        {
            return View();
        }

        public ActionResult ActionForm(int? id)
        {
            return ActionBase(id, true);
        }

        [HttpPost]
        public ActionResult ActionForm(CategoryModel model)
        {
            return ActionBase(model, true);
        }

        public ActionResult Delete(int id)
        {
            return DeleteBase(id, true);
        }

        public void FillViewBags(int? parentId = null)
        {
            var categories = new Category().SelectAllActive<Category>().OrderBy(m => m.Description);

            ViewBag.Parent = categories.ToSelectList(x => x.Name, x => x.Id.ToString(), "Select a parent category", parentId.HasValue ? parentId.Value.ToString() : string.Empty);
        }
    }
}
